import Vue from "vue";
import axios from "axios";
import { Toast } from "vant";
// import store from "../store/index";
import { getToken } from "@/utils/auth";
Vue.prototype.$axios = axios;

// 创建axios实例
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // api的base_url
  timeout: 150000000 // 请求超时时间
});
// request拦截器
service.interceptors.request.use(
  config => {
    config.headers["Authorization"] = getToken();
    // config.headers["Authorization"] = "9d79e5b9a33641b1a9C78e96a982baae";
    return config;
  },
  error => {
    console.log("request拦截器报错：" + error);
    Promise.reject(error);
  }
);

// respone拦截器
service.interceptors.response.use(
  response => {
    const res = response.data;
    if (res.status != 200) {
      if (res.status == 404) {
        Toast("找不到数据");
      } else if (res.status == 500) {
        Toast("服务器内部错误");
      }else if(res.status == 401){
        Toast("未经授权");
      }
      return Promise.reject(res);
    } else {
      return res;
    }
  },
  error => {
    Toast("网络错误");
    return Promise.reject(error);
  }
);

export default service;
